﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NLog;
using Entity.Client;
using DAL;
using System.IO;

public partial class AccountSettings : System.Web.UI.Page
{
    #region Variables
    private static Logger _logger = LogManager.GetCurrentClassLogger();
    #endregion


    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            Page.Title = "موقع لايف كورة | تعديل بيانات حسابي";
            Page.MetaDescription = "موقع لايف كورة | LiveKora.net | تعديل بيانات حسابي";
            if (!IsPostBack)
            {
                if (FormsFunction.GetLiveKoraCookie() != null)
                {
                    string[] arrUserInfo = FormsFunction.GetLiveKoraCookie();
                    RegUsers objRegUsers = new RegUsers();
                    RegisterUsers objRegisterUsers = objRegUsers.GetRegisterUsersByUserID(arrUserInfo[0].ToString());
                    MainUserP.Value = objRegisterUsers.Password;
                    if (string.IsNullOrEmpty(objRegisterUsers.FacebookID))
                    {
                        //td_FacebookMessage.Style.Add("display", "none");
                        txtUserEmailAddress.Text = objRegisterUsers.EmailAddress;
                        txtUserFullName.Text = objRegisterUsers.UserFullName;
                        if (string.IsNullOrEmpty(objRegisterUsers.UserImageProfile))
                        {
                            img_UserProfile.Src = "~/images/UserProfiles/LivekoraUnknowUser.png";
                        }
                        else
                        {
                            img_UserProfile.Src = objRegisterUsers.UserImageProfile;
                        }
                    }
                    else
                    {
                        //td_FacebookMessage.Style.Add("display", "");
                        txtUserEmailAddress.Text = objRegisterUsers.EmailAddress;
                        txtUserFullName.Text = objRegisterUsers.UserFullName;
                        txtUserEmailAddress.Enabled = false;
                        txtUserPassword.Enabled = false;
                        txtUserFullName.Enabled = false;
                        FileUpload1.Enabled = false;
                        img_UserProfile.Src = string.Format("https://graph.facebook.com/{0}/picture?type=large", objRegisterUsers.FacebookID);
                    }
                    txtUserInfo.Text = objRegisterUsers.UserBio;
                }
                else
                {
                }
            }
        }
        catch (Exception ex)
        {
            _logger.Error("AccountSettings:::Page_Load:::" + ex.Message);
            throw;
        }
    }

    protected void btnEditUserInfo_Click(object sender, EventArgs e)
    {
        try
        {
            string[] arrUserInfo = FormsFunction.GetLiveKoraCookie();

            RegUsers objRegUsers = new RegUsers();
            RegisterUsers objRegisterUsers = objRegUsers.GetRegisterUsersByUserID(arrUserInfo[0].ToString());

            objRegisterUsers.AuthType = objRegisterUsers.AuthType;
            objRegisterUsers.Country = objRegisterUsers.Country;
            objRegisterUsers.EmailAddress = txtUserEmailAddress.Text;
            objRegisterUsers.FacebookID = objRegisterUsers.FacebookID;
            objRegisterUsers.UserID = arrUserInfo[0].ToString();


            if (MainUserP.Value != EncryptionMethods.Encryption.Encrypt(txtUserPassword.Text))
            {
                objRegisterUsers.Password = objRegisterUsers.Password;
            }
            else
            {
                objRegisterUsers.Password = EncryptionMethods.Encryption.Encrypt(txtUserPassword.Text);
            }

            objRegisterUsers.UserFullName = txtUserFullName.Text;
            objRegisterUsers.UserStatus = 1;
            objRegisterUsers.UserBio = txtUserInfo.Text;
            
            bool bIsImageFileCorrect = false;
            if (string.IsNullOrEmpty(objRegisterUsers.FacebookID))
            {
               
                if (FileUpload1.HasFile)
                {
                    FileInfo objFileInfo = new FileInfo(FileUpload1.FileName);
                    if (objFileInfo.Extension.ToLower() == ".jpg" || objFileInfo.Extension.ToLower() == ".png" || objFileInfo.Extension.ToLower() == ".jpeg")
                    {
                        FileUpload1.SaveAs(Server.MapPath("~/images/UserProfiles/" + FileUpload1.FileName));
                        objRegisterUsers.UserImageProfile = "~/images/UserProfiles/" + FileUpload1.FileName;
                        bIsImageFileCorrect = true;
                    }
                    else
                    {
                        bIsImageFileCorrect = false;
                    }
                }
            }
            else
            {
                bIsImageFileCorrect = true;
            }

            if (bIsImageFileCorrect)
            {
                int nReturnUserID = objRegUsers.EditClientInfo(objRegisterUsers);
                if (nReturnUserID == 1)
                {
                    spUserRegisterMessage.InnerHtml = "لقد تم تعديل البينات بنجاح";
                    objRegisterUsers = objRegUsers.GetRegisterUsersByUserID(arrUserInfo[0].ToString());


                    if (string.IsNullOrEmpty(objRegisterUsers.FacebookID))
                    {
                        //td_FacebookMessage.Style.Add("display", "none");
                        txtUserEmailAddress.Text = objRegisterUsers.EmailAddress;
                        txtUserFullName.Text = objRegisterUsers.UserFullName;
                        MainUserP.Value = objRegisterUsers.Password;
                    }
                    else
                    {
                        //td_FacebookMessage.Style.Add("display", "");
                        txtUserEmailAddress.Text = objRegisterUsers.EmailAddress;
                        txtUserFullName.Text = objRegisterUsers.UserFullName;
                        txtUserEmailAddress.Enabled = false;
                        txtUserPassword.Enabled = false;
                        txtUserFullName.Enabled = false;
                        //btnEditUserInfo.Visible = false;
                    }
                    Response.Redirect("AccountSettings", false);
                }
                else
                {
                    spUserRegisterMessage.InnerHtml = "خطأ في تعديل البيانات";
                }
            }
            else
            {
                spUserRegisterMessage.InnerHtml = "الرجاء إدخال صورة ذات إمتداد .png , .jpg";
            }
        }
        catch (Exception ex)
        {
            _logger.Error("AccountSettings:::Page_Load:::" + ex.Message);
            spUserRegisterMessage.InnerHtml = "هناك مشكلة في تسجيل الحساب الرجاء المحاولة فيما بعد";
            throw;
        }
        tr_RegisterUser.Style.Add("display", "");

    }
}